import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/homepage/HomeView.vue'
import Layout from "@/views/layout.vue";
import Cookie from "js-cookie";

Vue.use(VueRouter)

const routes = [
    // ====== 登录 ======
    {
        path: '/login',
        name: 'Login',
        component: () => import('@/views/login/login.vue')
    },
    // ====== 主页 ======
    {
        path: '/',
        name: 'layout',
        component: Layout,
        redirect: '/home',
        children: [
            {
                path: 'home',
                name: 'Home',
                component: HomeView,
            },
            {
                path: 'about',
                name: 'About',
                // route level code-splitting
                // this generates a separate chunk (about.[hash].js) for this route
                // which is lazy-loaded when the route is visited.
                component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
            },
            {
                path: 'user',
                name: 'User',
                component: () => import('@/views/User/User.vue')
            },
            {
                path: 'addUser',
                name: 'AddUser',
                component: () => import('@/views/User/AddUser.vue')
            },
            {
                path: 'editUser',
                name: 'EditUser',
                component: () => import('@/views/User/EditUser.vue')
            },
            {
                path: 'admin',
                name: 'Admin',
                component: () => import('@/views/Admin/Admin.vue')
            },
            {
                path: 'addAdmin',
                name: 'AddAdmin',
                component: () => import('@/views/Admin/AddAdmin.vue')
            },
            {
                path: 'editAdmin',
                name: 'EditAdmin',
                component: () => import('@/views/Admin/EditAdmin.vue')
            },
        ]
    },
    {
        path: '*',
        component:() => import('@/views/404.vue')
    }

]

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes
})

// // 路由守卫
// router.beforeEach((to, from, next) =>{
//     if (to.path === '/login') next()
//     const admin = Cookie.get("admin")
//     if (!admin && to.path !== '/login') return next("login") // 强制退回到登录页面
//     next()
// })

export default router
